home *** CD-ROM | disk | FTP | other *** search
/ Developer Helper 1: Phil & Dave's Excellent CD / Excellent CD HFS.raw / Utilities / ResEdit / Examples / PExamples / Source / ICONPick.p < prev    next >
Text File  |  2022-08-05  |  7KB  |  219 lines

  1. {
  2.  COPYRIGHT (C) 1984-1989 Apple Computer,Inc.
  3.  All rights reserved
  4. }
  5.  
  6. UNIT IconPick;
  7.  
  8. { Icon Resource Picker }
  9.  
  10. { This is the Icon resource picker.  This picker is very similar to all other pickers.
  11.     The general scheme is that a List structure is created whose cells
  12.     contain the ID's of this resource type.  A drawproc is installed in the List
  13.     record which is called to display the resource.  Obviously, graphical resources
  14.     are drawn as is, but descriptions of other types can be displayed also (usually
  15.     in a one-dimensional list). }
  16.  
  17. INTERFACE
  18.  
  19. USES    MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
  20.             ResEd;
  21.  
  22. PROCEDURE EditBirth(thing: Handle; dad: ParentHandle);
  23. PROCEDURE PickBirth(t: ResType; dad: ParentHandle);
  24. PROCEDURE DoEvent(VAR evt: EventRecord; pick: PickHandle);
  25. PROCEDURE DoInfoUpdate(oldID, newID: INTEGER; pick: PickHandle);
  26. PROCEDURE DoMenu(menu, item: INTEGER; pick: PickHandle);
  27.  
  28. IMPLEMENTATION
  29.  
  30. CONST
  31.     listCellSizeH = $40;
  32.     listCellSizeV = $40;
  33.     theScrollBar    = 15;
  34.     
  35.     { Needs to be 2 wide so that I can always tell a 2d list from a normal text list. }
  36.     minIconsPerRow = 2;
  37.     ICONMinWindowWidth = (minIconsPerRow * listCellSizeH) + theScrollBar;
  38.     ICONMinWindowHeight = listCellSizeV;
  39.     
  40.     ICONResourceSize = 128;
  41.  
  42. {$R-}
  43.  
  44. PROCEDURE EditBirth(thing: Handle; dad: ParentHandle);
  45.  
  46.     BEGIN
  47.     END;
  48.  
  49. { *********************************************************************************** }
  50.  
  51. { Returns the width and the iconsPerRow. }
  52. FUNCTION GetWidth (VAR iconsPerRow: INTEGER): INTEGER;
  53.  
  54. VAR
  55.     width: INTEGER;
  56.  
  57. BEGIN
  58. width := PickStdWidth;
  59. IF width > ICONMinWindowWidth THEN
  60.     BEGIN
  61.     iconsPerRow := (width - theScrollBar) DIV listCellSizeH;
  62.     width := (iconsPerRow * listCellSizeH) + theScrollBar;
  63.     END
  64. ELSE
  65.     BEGIN
  66.     width := ICONMinWindowWidth;
  67.     iconsPerRow := minIconsPerRow;
  68.     END;
  69. GetWidth := width;
  70. END;
  71.  
  72. { *********************************************************************************** }
  73.  
  74. FUNCTION GetHeight: INTEGER;
  75.  
  76. VAR
  77.     height: INTEGER;
  78.  
  79. BEGIN
  80. height := PickStdRows * DefaultListCellSize;
  81. IF height > ICONMinWindowHeight THEN
  82.     height := (height DIV listCellSizeV) * listCellSizeV
  83. ELSE
  84.     height := ICONMinWindowHeight;
  85. GetHeight := height;
  86. END;
  87.  
  88. { *********************************************************************************** }
  89.  
  90. PROCEDURE PickBirth(t: ResType; dad: ParentHandle);
  91.  
  92. VAR
  93.     pick: PickHandle;
  94.     myWindow: WindowPtr;
  95.     from, myTitle: STR255;
  96.     cSize: Cell;
  97.     iconsPerRow: INTEGER;
  98.  
  99. BEGIN
  100. IF (t <> 'ICON') THEN
  101.     EXIT (Pickbirth);                                    { This only works for ICON resources. }
  102.     
  103. GetStr(fromStr, miscStrings, from); { Get ' from ' }
  104. myTitle := 'ICONs';
  105. ConcatStr (myTitle, from);
  106. myWindow := WindSetup(GetWidth (iconsPerRow), GetHeight, myTitle, dad^^.name);
  107.  
  108. IF myWindow <> NIL THEN
  109.     BEGIN
  110.     { Get the memory for my data structure. }
  111.     pick := PickHandle(NewHandle(SIZEOF(PickRec)));
  112.  
  113.     BubbleUp (Handle(pick));                    { Move it out of the way. }
  114.     HLock (Handle(pick));                            { Lock it down so that I can dereference it. }
  115.  
  116.     WITH pick^^ DO
  117.         BEGIN
  118.         father := dad;                                     { Back ptr to dad }
  119.         fName := dad^^.name;
  120.         wind := myWindow;                             { Directory window }
  121.         rebuild := FALSE;
  122.         pickID := ResedID;                             { Get my resource ID }
  123.         rType := 'ICON';                                 { Resource type that I understand }
  124.         rNum := CurResFile;                         { Owner res file }
  125.         rSize := ICONResourceSize;
  126.  
  127.         WITH WindowPeek(myWindow)^ DO
  128.             BEGIN
  129.             windowKind := ResedID;                { Get my resource ID }
  130.             refCon := ORD(pick);                    { Save the data structure for later use. }
  131.             END;
  132.  
  133.         cSize.h := listCellSizeH;
  134.         cSize.v := listCellSizeV;
  135.         instances := WindList(myWindow, iconsPerRow, cSize, ResedID);
  136.         
  137.         scroll := instances^^.vScroll;
  138.         
  139.         LDoDraw(FALSE, instances);                            { Don't draw it as it is built - too slow. }
  140.         nInsts := BuildType('ICON', instances); { Fill in the list with the ids of the resources. }
  141.         LSetSelect(true, Cell(0), instances);        { Select the first icon. }
  142.         LDoDraw(true, instances);                                { Next time, draw them. }
  143.  
  144.         { Need to update here to avoid the following:    The window is activated and the
  145.         user types a character which causes the list to scroll.  The activate event is processed
  146.         first and draws and hilights the first item.    The key down is processed next and scrolls
  147.         the window which has only one item drawn.  An update comes along which includes all but
  148.         the first item (which was updated by LActivate).    The display is left partially updated. }
  149.         BeginUpdate(myWindow);
  150.         LUpdate(myWindow^.visRgn, instances);
  151.         EndUpdate(myWindow);
  152.         END;
  153.  
  154.     HUnlock(Handle(pick));
  155.     END;
  156. END;
  157.  
  158. { Everything except the grow box is taken care of for us by PickEvent.}
  159. PROCEDURE DoEvent(VAR evt: EventRecord; pick: PickHandle);
  160.  
  161. VAR
  162.     windPtr: WindowPtr;
  163.  
  164. BEGIN
  165. { Must do our own grow box manipulation so that the min size is set correctly. }
  166. IF (evt.what = mouseDown) & (FindWindow(evt.where, windPtr) = inGrow) THEN
  167.     GrowMyWindow (ICONMinWindowWidth, ICONMinWindowHeight, windPtr, pick^^.instances)
  168. ELSE
  169.     PickEvent(evt, pick);
  170. END;
  171.  
  172. { Everything is taken care of for us by PickInfoUp }
  173. PROCEDURE DoInfoUpdate(oldID, newID: INTEGER; pick: PickHandle);
  174.  
  175. BEGIN
  176. PickInfoUp(oldID, newID, pick);
  177. END;
  178.  
  179. PROCEDURE DoMenu(menu, item: INTEGER; pick: PickHandle);
  180.  
  181. VAR
  182.      len, id: INTEGER;
  183.      tempH: Handle;
  184.      theCell: Cell;
  185. BEGIN
  186. { Since we loaded all of the icons to draw them, we need to release them when we close the window. }
  187. IF (menu = fileMenu) & (item = closeItem) THEN
  188.     BEGIN
  189.     { Must do the PassMenu here even though it is done again in PickMenu so that there are no
  190.         child windows open using the resources that I am about to free. }
  191.     PassMenu(fileMenu, closeItem, ParentHandle(pick));
  192.     
  193.     BubbleUp(Handle(pick));
  194.     HLock(Handle(pick));
  195.     WITH pick^^ DO
  196.         BEGIN
  197.         UseResFile(rNum);        { Make sure that we have the correct file. }
  198.         theCell := Cell(0);    { Start at the first cell. }
  199.         SetResLoad (FALSE);    { Don't load them in - we are trying to get rid of them! }
  200.         REPEAT                            { Loop through all of the cells. }
  201.             len := 2;                    { Get the id from the cell. }
  202.             LGetCell(@id, len, theCell, instances);
  203.             IF len > 0 THEN
  204.                 BEGIN
  205.                 tempH := Get1Res ('ICON', id);    { Get the resource if it is loaded. }
  206.                 IF tempH <> NIL THEN                        { If not null then it was loaded. }
  207.                     ReleaseResource (tempH);            { Toss it if it isn't changed. }
  208.                 END;
  209.          UNTIL NOT LNextCell (TRUE, TRUE, theCell, instances);
  210.          SetResLoad (TRUE);    { Always restore this! }
  211.         END;
  212.     END;
  213.  
  214. { Everything else is taken care of for us by PickMenu. }
  215. PickMenu(menu, item, pick);    { Do the normal picker stuff. }
  216. END;
  217.  
  218. END.
  219.